<?php
  require_once('./includes/header.php');

  if(!isset($_SESSION['id']) || empty($_SESSION['id'])){
    header("location:login.php");
    exit;
  }



 /***** DEFAULT START ********/
 $msg = getFlashMessage();
 $pn = 'show_all_users.php';
 /***** DEFAULT END ********/





 /*** Start Change Status***/
 //show_all_users.php?id=16&status=1
 if(isset($_GET['id'])
 && !empty($_GET['id'])
 && isset($_GET['status'])) {
   $id = $_GET['id'];
   $status = $_GET['status'];
   $newStatus = ($status == 0)? 1 : 0 ;
   /****************************************/
   updateById('users',$id,array('status'=>$newStatus));
   /****************************************/
 }
 /*** End Change Status ***/
 if(isset($_GET['id'])
 && !empty($_GET['id'])
 && isset($_GET['mode'])
 && $_GET['mode']=='delete') {
   $id = $_GET['id'];
  $rows = deleteById('users',$id);
  if($rows == 1) {
    $msg = 'Record has been deleted successfully.';
  }
 }
  ##############   WHERE ############
  $search = (isset($_GET['search'])&& !empty($_GET['search'])) ? $_GET['search'] : '';

  $where = trim($search);
  if($where !=''){
    $where = "`uname` LIKE '%$where%' OR `email` LIKE '%$where%'";
  }
  ############  END WHERE #########

  /************* PAGING **************/
  $totalUsers = countAll('users',$where);
  $rpp = 3; # a.k.a offset
  $totalPages = ceil($totalUsers / $rpp);
  $curPage = isset($_GET['page']) && !empty($_GET['page']) ? $_GET['page'] : 1;
  $index = ($curPage - 1) * $rpp;

  /************* PAGING **************/


  /***************************/
  $field = 'added_date';
  $order = 'desc';
  $orderBy = "$field $order";
  $icon = 'glyphicon-sort-by-attributes-alt';
  $newOrder = 'asc';
  if(isset($_GET['order']) && !empty($_GET['order']))
  {
    $field = $_GET['field'];
    $order = $_GET['order'];
    $orderBy = "$field $order";

    # $newOrder to be use in hyperlink of TD
    $newOrder = ($order == 'asc')?'desc':'asc';

    $icon = ($order == 'asc')?'glyphicon-sort-by-attributes':'glyphicon-sort-by-attributes-alt';
  }
  $result = fetchAll('users',$where,'',$orderBy,"$index, $rpp");

  /***************************/
?>

<div class="row">
<?php
if($msg != '') {
?>
    <div class="alert alert-success fade in">
      <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
      <p><?=$msg?></p>
    </div>
<?php
}
?>

<!-----------------------Search Form & Total users---------------->
<div>
    <ul style="" class=" nav nav-pills nav-tab navar-inverse">
    <li class="active">
      <a href="#">
        <span class="badge pull-right"><?=$totalUsers?></span>
        Total users
      </a>
    </li>
    <li class="pull-right ">
      <form class="navbar-form navbar-right" role="search" class="" method="get" action="">
        <div class="form-group">
          <input align="" type="text" class="form-control " placeholder="Search" name="search">
          <button type="submit" class="btn btn-default active">Submit</button>
        </div>
      </form>
    </li>
    <li class=" active">
      <a href="export.php?tbl=users">Export <span class="glyphicon glyphicon-export"></span></a>
      </li>
      <li class=" active">
      <a href="import.php?tbl=users">Import <span class="glyphicon glyphicon-import"></span></a>
    </li>
    
  </ul>
</div>


<!--------------------end of search form---------------------------->
  <table class="table table-responsive table-bordered table-hover">
    <thead class="bg-info">
      <tr>
        <th>#</th>
        <th>Name
<a href="<?="$pn?field=uname&order=$newOrder&search=$search&page=$curPage"?>">
        <span class="glyphicon <?=$icon?>"></span>
</a>

        </th>
        <th>Email

<a href="<?="$pn?field=email&order=$newOrder&search=$search&page=$curPage"?>">
        <span class="glyphicon <?=$icon?>"></span>
</a>

        </th>
        <th>D.O.B</th>
        <th>Gender</th>
        <th>Avatar</th>
        <th>Added Date
<a href="<?="$pn?field=added_date&order=$newOrder&search=$search&page=$curPage"?>">
        <span class="glyphicon <?=$icon?>"></span>
</a>
        </th>
        <th>Status
<a href="<?="$pn?field=status&order=$newOrder&search=$search&page=$curPage"?>">
        <span class="glyphicon <?=$icon?>"></span>
</a>
        </th>
        <th>Action</th>
      </tr>
    </thead>
    <tbody>
<?php
while($ans = mysql_fetch_assoc($result)){
    $id = $ans['id'];
    $src = './uploads/'.$ans['avatar'];
    $thumbSrc = './uploads/wall/'.$ans['avatar'];
    $status = $ans['status'];
?>

      <tr>
        <td><?=$ans['id']?></td>
        <td><?=$ans['uname']?></td>
        <td><a href="mailto:<?=$ans['email']?>"><?=$ans['email']?></a></td>
        <td><?=$ans['dob']?></td>
        <td><?=getGender($ans['gender'])?></td>
        <td>
          <a href="<?=$src?>" class="fancybox-thumbs thumbnail">
            <img class="img-thumbnail"
            src="<?=$thumbSrc?>" alt="user's Image">
          </a>
        </td>
        <td><?=date('d-M-Y',strtotime($ans['added_date']))?></td>
        <td><a href="<?="$pn?id=$id&status=$status"?>">
          <?=getStatus($status)?>
          </a>
        </td>
        <td>

          <a href="edit_user.php?id=<?=$id?>"><span class="glyphicon glyphicon-edit"></span></a>


          <a href="#" data-id ="<?=$id?>" class="deleteUser"><span class="glyphicon glyphicon-trash"></span></a>
        </td>
      </tr>
<?php
}// end while
?>

    </tbody>
  </table>

  <div>
    <ul class="pagination pagination-lg">
      <li><a href="#">«</a></li>
<?php
for($i=1 ; $i<=$totalPages ; $i++)
{

  $class = $curPage==$i ? 'active':'inactive';
?>
      <li class="<?=$class?>">
      <a href="<?="$pn?page=$i&search=$search&order=$order&field=$field"?>">
      <?=$i?>
      </a></li>
<?php
}//end for()
?>

      <li><a href="#">»</a></li>
    </ul>
  </div>


</div>





  <!-- Small modal -->
  <!-- <button class="btn btn-primary" data-toggle="modal" data-target=".bs-example-modal-sm">Small modal</button> -->

  <div class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-sm">
      <div class="modal-content">
        <div class="modal-header">
          <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
          <h4 class="modal-title">Delete</h4>
        </div>
        <div class="modal-body">
          <p>Are you sure you want to delete that user.</p>
        </div>
        <div class="modal-footer">
          <button data-dismiss="modal" class="btn btn-default" type="button">Close</button>
          <a id="modelDeleteId" href="#">
          <button class="btn btn-primary" type="button">Delete</button>
          </a>
        </div>
      </div>
    </div>
  </div><!-- end of model for delete --->


<script type="text/javascript">
   var pn = '<?=$pn?>';

  $(document).ready(function(){
    jQuery('.deleteUser').on('click',function(){

      var id = jQuery(this).data('id');
      //alert(id);

      $('.bs-example-modal-sm').modal('show');
      var link = pn+'?id='+id+'&mode=delete';
      $('#modelDeleteId').attr('href',link);
    })
  })
</script>
<!-- Add fancyBox main JS and CSS files -->
  <script type="text/javascript" src="./fancyapps/source/jquery.fancybox.js?v=2.1.5"></script>
  <link rel="stylesheet" type="text/css" href="./fancyapps/source/jquery.fancybox.css?v=2.1.5" media="screen" />

  <script type="text/javascript">
  jQuery(document).ready(function(){
    /*
    * Thumbnail helper. Disable animations, hide close button, arrows and slide to next gallery item if clicked
    */
    $('.fancybox-thumbs').fancybox({
        prevEffect : 'none',
        nextEffect : 'none',
        closeBtn :true,
        arrows : false,
        nextClick : true,
        helpers : {
          thumbs : {
            width : 50,
            height : 50
          }
        }
     });

  })
  </script>

<?php
  require_once('./includes/footer.php');
?>
